﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillRepurchaseService
    {
        /// <summary>
        /// 查询采购退货单
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="repurchaseno">退货单号</param>
        /// <returns></returns>
        public BillRepurchase GetBillRepurchase(long id, String repurchaseno)
        {
            BillRepurchase repurchase = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    repurchase = new BillRepurchase();
                    repurchase.Id = id;
                    repurchase.RepurchaseNo = repurchaseno;
                    repurchase = new BillRepurchaseDAO().GetBillRepurchase(conn, repurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return repurchase;
        }

        /// <summary>
        /// 查询采购退货单列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面大小</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="repurchaseno">退货单号</param>
        /// <param name="purchaseno">采购单号</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="isRefund">是否退款</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="ascfield">升序字段</param>
        /// <param name="descfield">降序字段</param>
        /// <returns></returns>
        public DataTable GetBillRepurchaseTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String repurchaseno,
            String purchaseno, String adminUsername, bool? isRefund, String startDate, String endDate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillRepurchase repurchase = new BillRepurchase();
                    repurchase.Pageindex = pageindex;
                    repurchase.Pagesize = pagesize;
                    repurchase.EnterpriseNo = enterpriseNo;
                    repurchase.StoreNo = storeNo;
                    repurchase.RepurchaseNo = repurchaseno;
                    repurchase.PurchaseNo = purchaseno;
                    repurchase.AdminUsername = adminUsername;
                    repurchase.IsRefund = isRefund;
                    repurchase.StartDate = startDate;
                    repurchase.EndDate = endDate;
                    repurchase.Ascfield = ascfield;
                    repurchase.Descfield = descfield;
                    dt = new BillRepurchaseDAO().GetBillRepurchaseTable(conn, repurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询采购退货单记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="repurchaseno">退货单号</param>
        /// <param name="purchaseno">采购单号</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="isRefund">是否退款</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns></returns>
        public long GetBillRepurchaseRecord(String enterpriseNo, String storeNo, String repurchaseno,
            String purchaseno, String adminUsername, bool? isRefund, String startDate, String endDate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillRepurchase repurchase = new BillRepurchase();
                    repurchase.EnterpriseNo = enterpriseNo;
                    repurchase.StoreNo = storeNo;
                    repurchase.RepurchaseNo = repurchaseno;
                    repurchase.PurchaseNo = purchaseno;
                    repurchase.AdminUsername = adminUsername;
                    repurchase.IsRefund = isRefund;
                    repurchase.StartDate = startDate;
                    repurchase.EndDate = endDate;
                    record = new BillRepurchaseDAO().GetBillRepurchaseRecord(conn, repurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增采购退货单
        /// </summary>
        /// <param name="repurchase"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public long InsertBillRepurchase(BillRepurchase repurchase, ArrayList list)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillRepurchaseDAO().InsertBillRepurchase(conn, repurchase);
                    if (id > 0 && list != null)
                    {
                        BillRepurchaseGoodsDAO repurchaseGoodsDAO = new BillRepurchaseGoodsDAO();
                        foreach (BillRepurchaseGoods lopRepurchaseGoods in list)
                        {
                            repurchaseGoodsDAO.InsertBillRepurchaseGoods(conn, lopRepurchaseGoods);
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新采购退货单
        /// </summary>
        /// <param name="repurchase"></param>
        /// <returns></returns>
        public int UpdateBillRepurchase(BillRepurchase repurchase)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new BillRepurchaseDAO().UpdateBillRepurchase(conn, repurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除采购退货单
        /// </summary>
        /// <param name="repurchasenos"></param>
        /// <returns></returns>
        public int DeleteBillRepurchase(String repurchasenos)
        {
            int count = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    if (!String.IsNullOrEmpty(repurchasenos))
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        String[] arr = repurchasenos.Split(new Char[] { ',' });
                        BillRepurchaseDAO repurchaseDAO = new BillRepurchaseDAO();
                        BillRepurchase repurchase = new BillRepurchase();
                        BillRepurchaseGoodsService repurchaseGoodsService = new BillRepurchaseGoodsService();
                        foreach (String lopRepurchaseno in arr)
                        {
                            repurchase.PurchaseNo = lopRepurchaseno;
                            int flag = repurchaseDAO.DeleteBillRepurchase(conn, repurchase);
                            if (flag > 0)
                            {
                                repurchaseGoodsService.DeleteBillRepurchaseGoods(conn, lopRepurchaseno);
                                count++;
                            }
                        }
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return count;
        }
    }
}
